<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/11/22
 * Time: 11:38
 */
namespace api\controllers;

use Yii;
use common\services\tool\ToolUrl;
use common\services\config\Config;
use common\services\ToolStringService;

class UrlManageController extends BaseController
{
    /**
     * URL加密 base64/encode
     * @return array
     */
    public function actionUrlEncode()
    {
        $urlStr = trim($this->request->post('url_str', ''));
        if (ToolStringService::mbStr($urlStr) > 100000) return $this->errorResult(Config::$no);

        $urlEncode = ToolUrl::urlEncode($urlStr);
        return $this->endReponse('0',[
            'url_encode' => $urlEncode
        ]);
    }

    /**
     * URL解密密 UrlDecode
     * @return array
     */
    public function actionUrlDecode()
    {
        $urlStr = trim($this->request->post('url_str', ''));
        if (ToolStringService::mbStr($urlStr) > 100000) return $this->errorResult(Config::$no);

        $urlDecode = ToolUrl::urlDecode($urlStr);
        return $this->endReponse('0',[
            'url_decode' => $urlDecode
        ]);
    }

    /**
     * URL加密 base64/encode
     * @return array
     */
    public function actionBase64Encode()
    {
        $urlStr = trim($this->request->post('url_str', ''));
        if (ToolStringService::mbStr($urlStr) > 100000) return $this->errorResult(Config::$no);

        $base64Encode = ToolUrl::base64Encode($urlStr);
        return $this->endReponse('0',[
            'base64_encode' => $base64Encode
        ]);
    }


    /**
     * URL解密 base64Decode
     * @return array
     */
    public function actionBase64Decode()
    {
        $urlStr = trim($this->request->post('url_str', ''));
        if (ToolStringService::mbStr($urlStr) > 100000) return $this->errorResult(Config::$no);
        $isBase64 = base64_decode($urlStr, true);
        if (!$isBase64) return $this->errorResult(Config::$notBase64);

        $base64Decode = ToolUrl::base64Decode($urlStr);
        return $this->endReponse('0',[
            'base64_decode' => $base64Decode
        ]);
    }
}